package com.geek.ch06.anno;

import com.geek.entity.Orders;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface OrdersMapper {

    //通过用户ID，查询其所有的订单信息
    @Select("select * from orders where user_id = #{uid}")
    @Results({
            @Result(id = true , property = "id" , column = "id"),
            @Result(column = "id" , property = "orderDetails" , javaType = List.class ,
                many = @Many(select = "com.geek.ch06.anno.OrderDetailMapper.findDetailsByOid"))
    })
    public List<Orders> findOrdersByUid(int uid);


    /**
     * 作业2：根据订单ID查询订单及其包含的商品信息 OM
     * @param oid 订单ID
     * @return 订单信息，包含订单明细和商品信息
     */
    @Select("select * from orders where id = #{oid}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "number", column = "number"),
            @Result(property = "createtime", column = "createtime"),
            @Result(property = "note", column = "note"),
            @Result(column = "id", property = "orderDetails", javaType = List.class,
                    many = @Many(select = "com.geek.ch06.anno.OrderDetailMapper.findDetailsByOid"))
    })
    public Orders findOrderWithItemsById(int oid);

}
